import {
  Body,
  Controller,
  Delete,
  Get,
  Param,
  Post,
  Put,
  Query,
  Req,
  UseInterceptors,
} from '@nestjs/common';
import { DeptService } from './dept.service';
import { DeptForm } from './dto/dept.form';
import {
  AuthInterceptor,
  RequiredPermission,
} from '../../config/filter/auth.interceptor';

@Controller('/api/v1/dept')
export class DeptController {
  constructor(private deptService: DeptService) {}

  /**
   * 部门列表-树形结构
   */
  @Get('')
  @UseInterceptors(AuthInterceptor)
  async listDepartments(@Query('keywords') keywords: string, status: number) {
    const result = this.deptService.listDepartments(keywords, status);
    return result;
  }

  /**
   * 新增部门信息
   * @param form
   * @param req
   */
  @Post('')
  @UseInterceptors(AuthInterceptor)
  @RequiredPermission('sys:dept:add')
  async addDepartment(@Body() form: DeptForm, @Req() req) {
    const result = this.deptService.addDepartment(form, req.user);
    return result;
  }

  /**
   * 获取部门下拉选项
   */
  @Get('/options')
  async listDeptOptions() {
    const result = this.deptService.listDeptOptions();
    return result;
  }

  /**
   * 修改部门信息
   * @param id
   * @param form
   * @param req
   */
  @Put('/:id')
  @UseInterceptors(AuthInterceptor) // token验签拦截器
  @RequiredPermission('sys:dept:edit')
  async updateDept(
    @Param('id') id: number,
    @Body() form: DeptForm,
    @Req() req,
  ) {
    const result = this.deptService.updateDept(id, form, req.user);
    return result;
  }

  /**
   * 删除
   * @param ids
   * @param req
   */
  @Delete('/:ids')
  @UseInterceptors(AuthInterceptor) // token验签拦截器 /**
  @RequiredPermission('sys:dept:delete')
  async deleteDept(@Param('ids') ids: string, @Req() req) {
    const result = this.deptService.deleteDept(ids, req.user);
    return result;
  }

  /**
   * 部门表单信息
   * @param id
   */
  @Get('/:id/form')
  @UseInterceptors(AuthInterceptor) // token验签拦截器
  async getForm(@Param('id') id: number) {
    const result = this.deptService.getForm(id);
    return result;
  }
}
